home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
019a
/
trim101.zip
/
TRIM11.C
next >
Wrap
C/C++ Source or Header
|
1991-11-06
|
6KB
|
158 lines
/********** trim.c ***** TurboC v1 nov91
* use f:\> trim [path]filespec.ext <cr> (for ascii text files)
* Pgm removes all trail blanks and trailing tabs from each line.
* Also replaces each internal tab with 3 blank spaces. The original ascii
* file is altered & written to a temp file, then deleted. The temp file is
* renamed to original name. Transparent to user.
* ken shirazee, mississauga, on, canada member Cda Remote Systems BBS
* v1.1 nov 06/91 Sorry, v1.0 of nov 05/91 had bug.
***** */
#include <stdio.h>
#include <string.h>
#define toascii(c) ((c) & 0x7f)
int _Cdecl unlink(char *filename);
char *workchar;
char *blankchar = " ";
main ( int argc, char *argv[] )
{
int position; /* VARS for create newfspec */
int dotfound = 0; /* = 0 if no '.' found in argv[1] */
char orifspec[65]; /* copy of argv[1] on cmd line, fnam.ext to trim */
char tmpfspec[65]; /* part of fnam, without .ext */
char newfspec[65]; /* becomes fnam.!T! for temporary file to create */
/* and later to rename as orifspec */
/* VARS for write trimmed and de-tabbed lines */
FILE *fp1, /* ptr to orifspec, asciifil.ext, to be read */
*fpout; /* ptr to newfspec, to be written-to */
char oneword[100], /* string, line read from asciifil.ext */
newword[100], /* string where each tab is replaced w 3 blanks */
outword[100]; /* line w no trail blanks, written to temp file */
int x, i, destlen;
if (argc < 2) /* f:\> trim <cr> is error, no input filename */
{
fprintf(stderr,"\tuse: c:\\> trim [path\\]ASCIIfil.ext <cr>\n");
fprintf(stderr,"\t or c:\\> trim ? <cr> for info");
exit(-1) ;
}
if (stricmp(argv[1],"?") == 0) /* trim ? on cmd line */
{
fprintf(stderr," usage is: c:\\> trim [path\\]ASCIIfil.ext <cr>\n");
fprintf(stderr,"\twhere ASCIIfil.ext is a TEXT file to be 'trimmed'.\n");
fprintf(stderr," TRIM.com removes trailing blanks & tabs on lines from\
input file;\n");
fprintf(stderr," also converts each internal tab to 3 blanks. Output\
is written to\n");
fprintf(stderr," a TEMPORARY file. The input file is deleted;\n");
fprintf(stderr," then the temporary file is RENAMED, as original input\
name.\n");
exit(-1) ;
}
fp1 = fopen(argv[1], "r"); /* ptr to adr of input fname, to read */
if (fp1 == NULL )
{
fprintf(stderr,"\tuse: c\\> trim [path\\]ASCIIfil.ext <cr>\n");
fprintf(stderr,"\t\t< %s > NOT FOUND.\n", argv[1]);
fclose(fp1);
exit(-1) ;
}
strcpy(orifspec, *++argv); /* copy the argv to orifspec */
/* look for position of LAST '.' in orifspec, if any */
for ( x=0,i=0; x <= strlen(orifspec); x++,i++ )
{
if ( 46 == ( toascii(orifspec[x]) ) ) /* dec value of '.' */
{
position = x + 1; /* position of LAST '.' */
dotfound = dotfound + 1; /* add 1 for any '.' found */
tmpfspec[i] = orifspec[x];
}
else
tmpfspec[i] = orifspec[x]; /* no '.' in orifspec, ie argv[1] */
}
if (dotfound == 1 ) /* only one '.' found in path AND filename */
{
strncpy(newfspec, tmpfspec, position - 1); /* so .ext excluded */
newfspec[position - 1] = '\0'; /* add nul terminator before add .!T! */
strcat(newfspec,".!T!");
}
if (dotfound == 0 ) /* no '.' in argv[1] */
{
strcpy(newfspec, tmpfspec);
newfspec[i] = '\0'; /* add nul terminator before add .!T! */
strcat(newfspec,".!T!");
}
if (dotfound == 2 ) /* two '.' found in argv[1] */
{
strncpy(newfspec, tmpfspec, position - 1); /* so .ext excluded */
newfspec[position - 1] = '\0'; /* add nul terminator before add .!T! */
strcat(newfspec,".!T!");
}
fpout = fopen(newfspec,"w");
if (fpout == NULL )
{
printf("\nDebug 4, can't open temporary file, < %s > to write to\n",newfspec);
printf("\ncan't open temporary file, < %s > to write to\n",fpout);
exit(0);
}
/* BIG LOOP *
* get 1 line at-a-time from user entered name input file, ends in 0A/0D.
* Reverse 'oneword' to locate position of 1st non-blank/non-tab.
*/
while ( fgets(oneword,100,fp1) != NULL)
{
strrev(oneword);
for (x=1; x <= strlen(oneword) ; x++ ) /* nb x=1 */
{
*workchar = oneword[x];
if ((stricmp(workchar, " ") != 0 ) /* if a non-space AND a */
&& (stricmp(workchar, " ") != 0 )) /* non-tab (ctrl I in quote) */
goto LABEL1;
}
LABEL1:
strrev(oneword); /* oneword reversed back to orig, [x] =last char */
destlen = strlen(oneword) - x -1 ;
/* ROUTINE TO replace 1 TAB (decimal 9) with 3 blanks in 'oneword',
* copy each char to make new string 'newword' */
for ( x=0, i=0; x <= destlen ; x++, i++ )
{
if ( 9 == ( toascii(oneword[x]) ) )
{
newword[i] = *blankchar;
newword[i+1] = *blankchar;
newword[i+2] = *blankchar;
i = i + 2; /* when 3 blanks per tab */
}
else
newword[i] = oneword[x];
}
strncpy(outword, newword, i );
outword[i] = '\n' ; /* add 0A/0D */
outword[i + 1] = '\0' ; /* add nul terminator */
fprintf(fpout, outword); /* write to temporary file */
} /* endo while */
fclose(fp1); /* close input file */
fclose(fpout); /* close temporary file */
unlink(orifspec); /* DELETE input filename given on cmd line */
rename(newfspec, orifspec); /* RENAME temp file to orig input filename */
}